package quesGenerate;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class filesHelper {
    private static int curr ,num;
    private static File question ,answer;
    private static FileWriter questionWriter ,answerWriter;
    // 默认构造函数捏，随便写的捏，别用
    public filesHelper() {
        num = 8;
        curr = 0;
    }
    // 使用重载构造函数
    public filesHelper(String filename) throws IOException {
        num = 8;
        curr = 0;
        question = new File("files/"+filename+".txt");
        answer = new File("files/"+filename+"_res.txt");
        questionWriter = new FileWriter(question);
        answerWriter = new FileWriter(answer);
    }
    // 文件写入
    public static void fileWrite(String ques) throws IOException {
        curr = curr+1;
        // 没有文件则创建同名文件
        if(!question.exists())  question.createNewFile();
        if(!answer.exists())    answer.createNewFile();
        // 写入题目文件和答案文件
        questionWriter.write(ques.substring(0,11));
        answerWriter.write(ques.substring(12)+" ");
        // 到指定位置换行
        if(curr == num){
            questionWriter.write("\n");
            answerWriter.write("\n");
            curr = 0;
        }
        // 将现有缓冲区数据强制写入文件，清空缓冲区
        questionWriter.flush();
        answerWriter.flush();
    }
    //关闭文件，程序最后使用捏
    public static void fileClose() throws IOException {
        questionWriter.close();
        answerWriter.close();
    }
    // 设置每行写几道题
    public static void setCurr(int numSet) {
        num = numSet;
    }
}
